import random
import uuid

from 阿里电商搜索.models.product import Product


class DataGenerator:
    def __init__(self):
        # 预定义的模拟数据
        self.categories = [
            "电子产品", "服装鞋帽", "家居用品", "美妆个护", "母婴用品",
            "食品饮料", "运动户外", "箱包配饰", "玩具乐器", "图书音像"
        ]

        self.brands = {
            "电子产品": ["华为", "苹果", "三星", "小米", "OPPO", "VIVO", "索尼", "联想", "戴尔", "惠普"],
            "服装鞋帽": ["耐克", "阿迪达斯", "李宁", "安踏", "优衣库", "ZARA", "H&M", "杰克琼斯", "ONLY", "VEROMODA"],
            "家居用品": ["宜家", "无印良品", "小米有品", "网易严选", "全友家居", "顾家家居", "索菲亚", "九牧", "箭牌",
                         "美的"],
            "美妆个护": ["兰蔻", "雅诗兰黛", "SK-II", "欧莱雅", "资生堂", "迪奥", "香奈儿", "纪梵希", "芙丽芳丝",
                         "曼秀雷敦"],
            "母婴用品": ["贝亲", "好孩子", "帮宝适", "好奇", "惠氏", "雅培", "美赞臣", "迪士尼", "澳贝", "费雪"],
            "食品饮料": ["可口可乐", "百事可乐", "雀巢", "蒙牛", "伊利", "旺旺", "三只松鼠", "良品铺子", "海底捞",
                         "双汇"],
            "运动户外": ["迪卡侬", "哥伦比亚", "探路者", "The North Face", "骆驼", "李宁", "安踏", "耐克", "阿迪达斯",
                         "佳明"],
            "箱包配饰": ["新秀丽", "外交官", "蔻驰", "MK", "TUMI", "金利来", "皮尔卡丹", "维多利亚的秘密", "施华洛世奇",
                         "潘多拉"],
            "玩具乐器": ["乐高", "迪士尼", "万代", "美泰", "孩之宝", "雅马哈", "卡西欧", "敦煌", "红棉", "罗兰"],
            "图书音像": ["中信出版社", "人民文学出版社", "机械工业出版社", "电子工业出版社", "清华大学出版社", "新东方",
                         "外研社", "读客", "磨铁", "果麦"]
        }

        self.product_keywords = {
            "电子产品": ["手机", "电脑", "平板", "耳机", "音响", "相机", "手表", "电视", "充电器", "充电宝"],
            "服装鞋帽": ["T恤", "衬衫", "裤子", "裙子", "外套", "卫衣", "运动鞋", "皮鞋", "凉鞋", "帽子"],
            "家居用品": ["沙发", "床", "桌椅", "灯具", "厨具", "餐具", "收纳", "清洁", "家纺", "电器"],
            "美妆个护": ["口红", "粉底液", "眼影", "香水", "面霜", "洗发水", "沐浴露", "牙膏", "牙刷", "剃须刀"],
            "母婴用品": ["奶粉", "纸尿裤", "奶瓶", "婴儿车", "安全座椅", "玩具", "童装", "童鞋", "孕妇装", "早教"],
            "食品饮料": ["零食", "坚果", "饼干", "巧克力", "饮料", "牛奶", "咖啡", "茶", "方便面", "罐头"],
            "运动户外": ["运动鞋", "运动服", "背包", "帐篷", "睡袋", "登山鞋", "自行车", "跑步机", "哑铃", "瑜伽垫"],
            "箱包配饰": ["行李箱", "背包", "手提包", "钱包", "皮带", "眼镜", "手表", "项链", "手链", "耳环"],
            "玩具乐器": ["积木", "拼图", "遥控车", "娃娃", "乐器", "吉他", "钢琴", "小提琴", "尤克里里", "鼓"],
            "图书音像": ["小说", "教材", "工具书", "杂志", "漫画", "CD", "DVD", "电子书", "有声书", "字帖"]
        }

        self.adjectives = ["时尚", "新款", "经典", "流行", "高品质", "实用", "舒适", "耐用", "环保", "智能"]

        # 电子产品描述模板
        self.electronics_descriptions = [
            "这款{adjective}{product_name}采用了最新的{technology}技术，拥有{feature}功能，性能卓越，是您工作和娱乐的好帮手。",
            "{adjective}{product_name}配备了{spec}，运行速度快，续航能力强，满足您的各种需求。",
            "全新{adjective}{product_name}，{design}设计，手感舒适，操作简单，让您的生活更加便捷。",
            "{adjective}{product_name}拥有{display}显示屏，色彩鲜艳，视觉效果出色，带给您沉浸式体验。",
            "这款{adjective}{product_name}采用了{material}材质，坚固耐用，同时具备{special_feature}特性，值得拥有。"
        ]

        # 服装鞋帽描述模板
        self.clothing_descriptions = [
            "这款{adjective}{product_name}采用了{material}面料，穿着舒适，透气吸汗，适合{occasion}穿着。",
            "{adjective}{product_name}设计时尚，剪裁合身，展现您的个性魅力，是{season}必备单品。",
            "全新{adjective}{product_name}，{color}配色，简约大方，搭配性强，让您的穿搭更加出彩。",
            "{adjective}{product_name}采用了{technology}技术，具有{feature}功能，让您在{activity}中更加舒适自在。",
            "这款{adjective}{product_name}做工精细，细节处理到位，穿着舒适，是您的理想选择。"
        ]

        # 家居用品描述模板
        self.home_descriptions = [
            "这款{adjective}{product_name}采用了{material}材质，坚固耐用，设计简约，适合各种家居风格。",
            "{adjective}{product_name}功能多样，使用方便，能够帮助您轻松完成{task}，提高生活品质。",
            "全新{adjective}{product_name}，{design}设计，节省空间，让您的家居环境更加整洁有序。",
            "{adjective}{product_name}采用了{technology}技术，具有{feature}功能，为您的生活带来更多便利。",
            "这款{adjective}{product_name}做工精细，品质优良，是您家居生活的好帮手。"
        ]

        # 美妆个护描述模板
        self.beauty_descriptions = [
            "这款{adjective}{product_name}富含{ingredient}成分，能够有效{effect}，让您的肌肤保持健康状态。",
            "{adjective}{product_name}采用了{technology}技术，质地轻盈，易于吸收，使用后肌肤感觉{feeling}。",
            "全新{adjective}{product_name}，{color}色号，显色度高，持久不脱妆，让您的妆容更加完美。",
            "{adjective}{product_name}温和不刺激，适合各种肤质使用，是您护肤美妆的好选择。",
            "这款{adjective}{product_name}包装精美，携带方便，是您日常护肤和出行的必备品。"
        ]

        # 其他类别描述模板
        self.other_descriptions = [
            "{adjective}{product_name}，品质优良，使用方便，是您{activity}的好帮手。",
            "全新{adjective}{product_name}，采用了{technology}技术，具有{feature}功能，满足您的需求。",
            "{adjective}{product_name}设计合理，做工精细，是您的理想选择。",
            "这款{adjective}{product_name}拥有{spec}，性能出色，让您的{activity}更加轻松愉快。",
            "{adjective}{product_name}，{material}材质，坚固耐用，为您提供长久的使用体验。"
        ]

    def generate_products(self, count):
        """生成指定数量的模拟产品数据"""
        products = []
        for _ in range(count):
            category = random.choice(self.categories)
            brand = random.choice(self.brands[category])
            keyword = random.choice(self.product_keywords[category])
            adjective = random.choice(self.adjectives)
            product_name = f"{adjective}{keyword}"

            # 根据类别选择描述模板
            if category == "电子产品":
                description_template = random.choice(self.electronics_descriptions)
                technology = random.choice(["5G", "无线充电", "高清", "智能", "防水"])
                feature = random.choice(["超长续航", "快速充电", "高清拍照", "大屏显示", "指纹识别"])
                spec = random.choice(["八核处理器", "128GB存储", "4K分辨率", "蓝牙5.0", "AI智能"])
                design = random.choice(["轻薄", "时尚", "简约", "精致", "便携"])
                display = random.choice(["6.5英寸", "4K", "全面屏", "AMOLED", "高刷新率"])
                material = random.choice(["金属", "玻璃", "塑料", "碳纤维", "铝合金"])
                special_feature = random.choice(["防水防尘", "无线充电", "折叠屏", "指纹解锁", "人脸识别"])

                description = description_template.format(
                    adjective=adjective,
                    product_name=product_name,
                    technology=technology,
                    feature=feature,
                    spec=spec,
                    design=design,
                    display=display,
                    material=material,
                    special_feature=special_feature
                )
            elif category == "服装鞋帽":
                description_template = random.choice(self.clothing_descriptions)
                material = random.choice(["纯棉", "涤纶", "羊毛", "丝绸", "亚麻"])
                occasion = random.choice(["日常", "运动", "工作", "休闲", "旅行"])
                season = random.choice(["春季", "夏季", "秋季", "冬季", "四季"])
                color = random.choice(["黑色", "白色", "红色", "蓝色", "灰色", "黄色", "绿色"])
                technology = random.choice(["防皱", "透气", "防水", "保暖", "抗菌"])
                feature = random.choice(["弹性好", "耐磨", "轻便", "防滑", "保暖"])
                activity = random.choice(["跑步", "健身", "登山", "办公", "约会"])

                description = description_template.format(
                    adjective=adjective,
                    product_name=product_name,
                    material=material,
                    occasion=occasion,
                    season=season,
                    color=color,
                    technology=technology,
                    feature=feature,
                    activity=activity
                )
            elif category == "家居用品":
                description_template = random.choice(self.home_descriptions)
                material = random.choice(["实木", "板材", "金属", "塑料", "玻璃"])
                task = random.choice(["清洁", "收纳", "烹饪", "休息", "娱乐"])
                design = random.choice(["简约", "现代", "欧式", "中式", "北欧"])
                technology = random.choice(["智能控制", "节能", "静音", "防水", "防霉"])
                feature = random.choice(["多功能", "可折叠", "易清洁", "大容量", "防滑"])

                description = description_template.format(
                    adjective=adjective,
                    product_name=product_name,
                    material=material,
                    task=task,
                    design=design,
                    technology=technology,
                    feature=feature
                )
            elif category == "美妆个护":
                description_template = random.choice(self.beauty_descriptions)
                ingredient = random.choice(["玻尿酸", "胶原蛋白", "维生素C", "茶树精油", "烟酰胺"])
                effect = random.choice(["保湿", "美白", "抗衰老", "控油", "修复"])
                technology = random.choice(["纳米技术", "微分子", "缓释", "植物萃取", "无添加"])
                color = random.choice(["自然色", "豆沙色", "姨妈色", "正红色", "橘色", "粉色"])
                feeling = random.choice(["清爽", "滋润", "舒适", "柔软", "光滑"])

                description = description_template.format(
                    adjective=adjective,
                    product_name=product_name,
                    ingredient=ingredient,
                    effect=effect,
                    technology=technology,
                    color=color,
                    feeling=feeling
                )
            else:
                description_template = random.choice(self.other_descriptions)
                technology = random.choice(["智能", "环保", "高效", "安全", "创新"])
                feature = random.choice(["多功能", "便携", "耐用", "节能", "静音"])
                spec = random.choice(["大容量", "高功率", "长续航", "高精度", "快速"])
                activity = random.choice(["工作", "学习", "生活", "旅行", "运动"])
                material = random.choice(["优质", "环保", "高强度", "耐磨", "防滑"])

                description = description_template.format(
                    adjective=adjective,
                    product_name=product_name,
                    technology=technology,
                    feature=feature,
                    spec=spec,
                    activity=activity,
                    material=material
                )

            # 生成价格 (10-10000之间的随机浮点数，保留两位小数)
            price = round(random.uniform(10, 10000), 2)

            # 生成评分 (1-5之间的随机浮点数，保留一位小数)
            rating = round(random.uniform(1, 5), 1)

            # 生成销量 (0-10000之间的随机整数)
            sales_volume = random.randint(0, 10000)

            # 生成标签
            tags = [keyword, brand, category]
            if adjective not in tags:
                tags.append(adjective)
            if "智能" in description or "AI" in description:
                tags.append("智能")
            if "防水" in description:
                tags.append("防水")
            if "无线" in description:
                tags.append("无线")
            if "便携" in description or "轻便" in description:
                tags.append("便携")
            if "时尚" in description or "新款" in description:
                tags.append("时尚")

            # 创建产品ID
            product_id = str(uuid.uuid4())

            # 创建产品对象
            product = Product(
                product_id=product_id,
                name=product_name,
                category=category,
                price=price,
                brand=brand,
                rating=rating,
                sales_volume=sales_volume,
                description=description,
                tags=tags
            )

            # 随机设置促销
            if random.random() < 0.3:  # 30%的商品有促销
                discount_rate = round(random.uniform(0.1, 0.5), 2)  # 10%-50%的折扣
                product.set_promotion(discount_rate)

            products.append(product)

        return products    